//
//  FurnitureData.m
//  Model
//
//  Created by YDJH on 2017/5/19.
//  Copyright © 2017年 YDJH. All rights reserved.
//

#define skRandom(from,to) (int)(from + (arc4random() % (to - from + 1))) //生成随机数[from,to]

#import "FurnitureData.h"
#import "HZCommand.h"
#import "HZFurniture_Header.h"
#import "RoomData.h"

@implementation FurnitureData

+ (NSMutableArray *)gobackFurnitureModel {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"FurnitureActions" ofType:@"plist"];
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
    NSMutableArray *mutableArr = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        FurnitureData *model = [[FurnitureData alloc] init];
        model.furnitureName = dict[@"name"];
        model.strength = dict[@"strength"];
        model.actionCollection = dict[@"action"];
        model.type = dict[@"type"];
        model.stateCollection = dict[@"state"];
        model.furnitureCategory = dict[@"category"];
        model.furniturePicture = dict[@"furniturePicture"];
        [mutableArr addObject:model];
    }
    return mutableArr;
}

+ (HZFurniture *)gobackFurnitureModelWithModel:(FurnitureData *)furniture {
    HZFurniture *model = [[HZFurniture alloc] init];
    model.furnitureName = furniture.furnitureName;
    model.furnitureType = furniture.type;
    model.actionCollection = furniture.actionCollection;
//    model.furnitureCategory = furniture.furnitureCategory;
    model.furnitureStrength = furniture.strength;
    
    model.furnitureState = furniture.stateCollection[skRandom(0,furniture.stateCollection.count-1)];
    model.furniturePicture = furniture.furniturePicture;
    return model;
}

+ ( HZFurniture *)gobackArrayWithFurnitureType:(NSString *)type {
    HZFurniture *furniture = [[HZFurniture alloc] init];
    
    return furniture;
}


//+ (NSArray <HZFurniture *>*)goBackArrayWithFutiniture:(NSArray *)array withFurData:(NSArray *)dataArr {
//    NSMutableArray *furnitureArray = [NSMutableArray array];
//    for (NSString *string in array) {
//        NSArray *furnitureInfo = [string componentsSeparatedByString:@","];
//    }
//}
//返回沙盘的显式家具
+ (NSArray <HZFurniture *>*)goBackArrayWithSandTableFutiniture:(NSArray *)array withFurData:(NSArray *)dataArr {
    
    NSMutableArray *furnitureArray = [NSMutableArray array];
    for (NSString *string in array) {
        NSArray *furnitureInfo = [string componentsSeparatedByString:@","];
        NSString *furnitureID = furnitureInfo[0];
        NSString *furniturePosition = [NSString stringWithFormat:@"%@,%@",furnitureInfo[1],furnitureInfo[2]];
        for (FurnitureData *furData in dataArr) {
            if ([furData.type isEqualToString:furnitureID]) {
                HZFurniture *furniture = [[HZFurniture alloc] init];
                furniture.position = furniturePosition;
                furniture.furnitureType = furnitureID;
                furniture.furnitureStrength = furData.strength;
                furniture.furnitureCategory = furData.furnitureCategory;
                furniture.furnitureName = furData.furnitureName;
                furniture.furnitureState = furData.stateCollection[skRandom(0,furData.stateCollection.count-1)];
                furniture.furniturePicture = furData.furniturePicture;
                NSMutableArray *commands = [NSMutableArray array];
                
                for (NSString *action in furData.actionCollection) {
                    HZCommand *command = [[HZCommand alloc] init];
                    command.type = action;
                    command.methodName = [NSString stringWithFormat:@"%@Action:withObject:",action];
                    command.commandName = action;
                    command.picture = [NSString stringWithFormat:@"%@command",action];
                    [commands addObject:command];
                }
                furniture.actionCollection = commands;
                
                [furnitureArray addObject:furniture];
                break;
            }
        }
    }
    return furnitureArray;
}
//返回沙盘上的隐式家具
+ (NSArray <HZHiddenFurniture *>*)goBackArrayWithSandTableHiddenFutiniture:(NSArray *)array withRoomData:(NSArray *)dataArr{
    
    NSMutableArray *hiddenFurArr = [NSMutableArray array];
    
    for (NSString *string in array) {
        NSArray *furnitureInfo = [string componentsSeparatedByString:@","];
        NSString *type = furnitureInfo[0];
        NSString *furniturePosition = [NSString stringWithFormat:@"%@,%@",furnitureInfo[1],furnitureInfo[2]];
        for (RoomData *roomData in dataArr) {
            
            if ([roomData.type isEqualToString:type]) {
                HZHiddenFurniture *hiddenFur = [[HZHiddenFurniture alloc] init];
                hiddenFur.position = furniturePosition;
                hiddenFur.picture = [NSString stringWithFormat:@"closedoor"];
                hiddenFur.buildingType = type;
                if ([roomData.roomCategory isEqualToString:@"3"]) {
                    hiddenFur.type = HiddenFurType_Building;
                }else {
                    hiddenFur.type = HiddenFurType_Outdoor;
                }
                
                [hiddenFurArr addObject:hiddenFur];
                break;
            }
        }
    }
    return hiddenFurArr;
}
//返回房间外墙的家具
+ (NSArray <HZFurniture *>*)goBackArrayWithOutsideFutiniture:(NSArray *)array withFurData:(NSArray *)dataArr indexPath:(NSIndexPath *)indexPath {
    
    NSMutableArray *furnitureArray = [NSMutableArray array];
    for (NSString *string in array) {
        NSArray *furnitureInfo = [string componentsSeparatedByString:@","];
        NSString *furnitureID = furnitureInfo[0];
        NSString *furniturePosition = [NSString stringWithFormat:@"%@,%@",furnitureInfo[1],furnitureInfo[2]];
        for (FurnitureData *furData in dataArr) {
            if ([furData.type isEqualToString:furnitureID]) {
                HZFurniture *furniture = [[HZFurniture alloc] init];
                furniture.position = furniturePosition;
                furniture.furnitureType = furnitureID;
                furniture.furnitureStrength = furData.strength;
                furniture.furnitureCategory = furData.furnitureCategory;
                furniture.furnitureName = furData.furnitureName;
                furniture.furnitureState = furData.stateCollection[skRandom(0,furData.stateCollection.count-1)];
                furniture.furniturePicture = furData.furniturePicture;
                furniture.indexPath = indexPath;
                furniture.outside = YES;
                NSMutableArray *commands = [NSMutableArray array];
                
                for (NSString *action in furData.actionCollection) {
                    HZCommand *command = [[HZCommand alloc] init];
                    command.type = action;
                    command.methodName = [NSString stringWithFormat:@"%@Action:withObject:",action];
                    command.commandName = action;
                    command.picture = [NSString stringWithFormat:@"%@command",action];
                    [commands addObject:command];
                }
                furniture.actionCollection = commands;
                
                [furnitureArray addObject:furniture];
                break;
            }
        }
    }
    return furnitureArray;
}
//返回房间内墙的家具
+ (NSArray <HZFurniture *>*)goBackArrayWithinsideFutiniture:(NSArray *)array withFurData:(NSArray *)dataArr indexPath:(NSIndexPath *)indexPath {
    
    NSMutableArray *furnitureArray = [NSMutableArray array];
    for (NSString *string in array) {
        NSArray *furnitureInfo = [string componentsSeparatedByString:@","];
        NSString *furnitureID = furnitureInfo[0];
        NSString *furniturePosition = [NSString stringWithFormat:@"%@,%@",furnitureInfo[1],furnitureInfo[2]];
        for (FurnitureData *furData in dataArr) {
            if ([furData.type isEqualToString:furnitureID]) {
                HZFurniture *furniture = [[HZFurniture alloc] init];
                furniture.position = furniturePosition;
                furniture.furnitureType = furnitureID;
                furniture.furnitureStrength = furData.strength;
                furniture.furnitureCategory = furData.furnitureCategory;
                furniture.furnitureName = furData.furnitureName;
                furniture.furnitureState = furData.stateCollection[skRandom(0,furData.stateCollection.count-1)];
                furniture.furniturePicture = furData.furniturePicture;
                furniture.indexPath = indexPath;
                furniture.outside = NO;
                NSMutableArray *commands = [NSMutableArray array];
                
                for (NSString *action in furData.actionCollection) {
                    HZCommand *command = [[HZCommand alloc] init];
                    command.type = action;
                    command.methodName = [NSString stringWithFormat:@"%@Action:withObject:",action];
                    command.commandName = action;
                    command.picture = [NSString stringWithFormat:@"%@command",action];
                    [commands addObject:command];
                }
                furniture.actionCollection = commands;
                
                [furnitureArray addObject:furniture];
                break;
            }
        }
    }
    return furnitureArray;
}

+ (BOOL)gobackBOOLWithSandTableFutiniture:(NSArray *)array {
    
    BOOL isShow = NO;
    
    for (HZFurniture *fur in array) {
        if ([fur.furnitureState isEqualToString:open_Type]||[fur.furnitureState isEqualToString:destory_Type]) {
            return YES;
        }
    }
    return isShow;
}


@end
